// 导航守卫
import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 前置守卫
const pageArr = ['/404', '/login'] // 记录不需要登录的 记录需要登录的页面path
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  if (store.getters.token) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      // 验证token,获取个人用户信息
      // 如果有用户信息不用获取,没有才获取
      if (!store.state.user.userInfo.userId) {
        await store.dispatch('user/getUserInfo')
      }
      next()
    }
  } else {
    if (pageArr.includes(to.path.toLowerCase())) {
      next()
    } else {
      // 浏览器兼容性问题 ,加上edcodeURIComponent  ie不支持中文字符
      next(`/login?redirect=${encodeURIComponent(to.fullPath)}`)
    }
  }
  NProgress.done()
})
// 后置守卫
router.afterEach((to, from) => {})
